package cn.school.config;

import cn.school.common.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

/**
 * 全局跨域配置
 */
@Configuration
@EnableWebMvc
public class GlobalWebConfig implements WebMvcConfigurer {
    @Resource
    private TokenInterceptor vueViewInterceptor;
    @Value("${school.imagePathStartDir}")
    private String filePath;
    @Value("${student.picturePathDir}")
    private String studentPicPath;
    @Value("${major.picturePathDir}")
    private String majorPicPath;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(vueViewInterceptor)
                .addPathPatterns("/**")
                // 排除配置
                .excludePathPatterns(
                        "/sys/tenant/findPageList",
                        "/workmgr/major/list/**",
                        "/workmgr/banner/list/**",
                        "/workmgr/major/listDetails/**",
                        "/workmgr/enroll/**",
                        "/admin/login",
                        "/app/login",
                        "/captchaImage",
                        "/model/*",
                        "/model/**/**",
                        "/editor/*",
                        "/test/*",
                        "/student/info/uploadImage",
                        "/workmgr/major/uploadImage",
                        "/swagger-resources/**",
                        "*.js",
                        "/**/*.js",
                        "*.css",
                        "/**/*.css",
                        "*.html",
                        "/v2/*",
                        "/**/*.html",
                        "/weChat/getOfficialOpenId","/weChat/saveOfficialOpenId","/weChat/saveTeacherOfficialOpenId",
                        "/external/interface/receiveDormitoryCheckInfo",
                        "/external/interface/receiveSchoolGateEntryAndExitInfo",
                        "/external/interface/getSomeInfoForOutByOpenId",
                        "/external/interface/receiveImgSecondPush");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("modeler.html")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/editor-app/**")
                .addResourceLocations("classpath:/static/editor-app/");
        registry.addResourceHandler("/diagram-viewer/**")
                .addResourceLocations("classpath:/static/diagram-viewer/");

        registry.addResourceHandler("/school/file/**").
                addResourceLocations("file:" + filePath);
        registry.addResourceHandler("/student/picture/**").
                addResourceLocations("file:" + studentPicPath);
        registry.addResourceHandler("/major/picture/**").
                addResourceLocations("file:" + majorPicPath);
    }
}
